﻿''''Last Modified on Nov 17, 2014 08.10 am
Imports ADOEntityKYC
Imports System.Data
Partial Class KYC_NonIndividualSearch
    Inherits Web.UI.BasePage

#Region "Valiable"
    Dim ds As DataSet
    Dim KYCEntity As KYCEntities = New KYCEntities
    Dim clsDataTable As New clsLINQToDataTable
    Dim convertDate As clsDateFormat
    Dim clsCheckLanguage As clsCheckLanguage
    Dim clsDatabase As clsDatabase
    Dim clsSessionMenuRight As clsSessionMenuRight = New clsSessionMenuRight
    Dim clsMasterDropDown As clsMasterDropDown
    Dim clsValid As clsValidateData
#End Region

#Region "Even Page"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        clsValid = New clsValidateData()
        Try
            clsSessionMenuRight = Session("clsMenuRight")
            btnSearch.Attributes.Add("onclick", "NoDoubleSubmit();")

            If Not IsPostBack Then
                ' Me.txtBizCode.Text = "%"
                chkIsLogin()
                ds = New DataSet()
                resourceSet()
                CreateControlMP()
                Session("KYC") = Request.QueryString("KYC")
                Session("CUSTSEQ") = Nothing
                'If Not Request.QueryString("CUSTSEQ") = 0 Or Not Request.QueryString("CUSTSEQ") = "" Then
                '    searchAuto(CInt(Request.QueryString("CUSTSEQ")))
                'End If

                If IsNumeric(Request.QueryString("CUSTSEQ")) Then
                    If Request.QueryString("CUSTSEQ") > 0 Then
                        searchAuto(CInt(Request.QueryString("CUSTSEQ")))
                    End If
                End If

                If Request.QueryString("KYC") = 4 Then 'เรืองระดับสิทธิ์การเห็นข้อมูล
                    loadDropDown()
                    lblBranch.Visible = True
                    ddlBranch.Visible = True
                End If
            Else
                ds = Session("ds")
            End If

            VisbleKYC()
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("ds") = ds
    End Sub
#End Region

#Region "SetForm"

    Sub resourceSet()

        lblNameTH.Text = Resources.resSearch.BizNameTH
        lblNameEN.Text = Resources.resSearch.BizNameEN
        lblBizCode.Text = Resources.resSearch.BizCode

        If Request.QueryString("KYC") = 1 Then
            chkType.Text = Resources.resSearch.DepositNonIndividual
        ElseIf Request.QueryString("KYC") = 2 Then
            chkType.Text = Resources.resSearch.LoanNonIndividual
        ElseIf Request.QueryString("KYC") = 3 Then
            chkType.Text = Resources.resSearch.PNNonIndividual
        ElseIf Request.QueryString("KYC") = 5 Then
            chkType.Text = Resources.resSearch.NPANonIndividual
        Else
            chkType.Text = Resources.resSearch.searchAdmin
        End If
        Dim clsDate As New clsDateFormat
        txtStartDate.Text = clsDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)
        txtEndDate.Text = clsDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)
    End Sub

    Sub CreateControlMP()
        Dim img As Image = CType(Master.FindControl("imgHead"), Image)
        img.ImageUrl = "~/img/LogoNonIndi.jpg"

        Dim page As Label = CType(Master.FindControl("lblPage"), Label)
        page.Text = "ค้นหาข้อมูล KYC"
    End Sub

#End Region

#Region " Even Button "

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Try
            clsCheckLanguage = New clsCheckLanguage
            btnAdd.Visible = True
            btnAddOld.Visible = False

            grdNonIndividual.SelectedIndex = -1
            If ds.Tables.Contains("dsCustomerComS") Then
                ds.Tables.Clear()
                grdNonIndividual.DataSource = Nothing
                grdNonIndividual.DataBind()
                btnEdit.Visible = False
                btnShow.Visible = False
                btnAdd.Enabled = False
            End If

            ''''validate Date ''''''''''''''''''''''''''''''
            If chkUser.Checked = True Then
                ''''''''''''
                If clsValid.CheckNull(Me.txtStartDate) = False Then Exit Sub

                If (clsValid.CheckDate(txtStartDate) = False) Then
                    Me.txtStartDate.Focus() : Exit Sub
                End If
                '''''''''
                If clsValid.CheckNull(Me.txtEndDate) = False Then Exit Sub
                If (clsValid.CheckDate(txtEndDate) = False) Then
                    Me.txtEndDate.Focus() : Exit Sub
                End If
                '''''''''''
                If DateDiff(DateInterval.Day, CDate(Me.txtEndDate.Text), CDate(Me.txtStartDate.Text)) > 0 Then
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('ข้อมูลไม่ถูกต้อง ! ช่วงวันที่สลับกัน');", True)
                    Me.txtEndDate.Focus()
                    Exit Sub
                End If
            End If

            If Not chkUser.Checked = True And Not Request.QueryString("KYC") = 4 Then

                ''''Prachak ''''''''''''''''''''''''''
                If Me.txtCIF_NO.Text.Trim & _
                   Me.txtBizCode.Text.Trim & _
                   Me.txtTAX_ID.Text.Trim & _
                   Me.txtTYPEEVIDENCE_NO.Text <> "" Then
                    If Me.txtNameTH.Text.Trim & _
                        Me.txtNameEN.Text.Trim <> "" Then
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alertMessage", "window.alert('กรุณาป้อนเลขที่บัตรประชาขน/เลขที่หนังสือเดินทาง หรือ ชื่อ-นามสกุลอย่างใดอย่างหนึ่ง');", True)
                        Exit Sub
                    End If
                End If
                ''''Prachak ''''''''''''''''''''''''''

                If Me.txtCIF_NO.Text = "" _
                   And Me.txtCustCode.Text.Trim = "" _
                   And txtBizCode.Text.Trim = "" _
                   And txtTYPEEVIDENCE_NO.Text.Trim = "" _
                   And txtNameTH.Text.Trim = "" _
                   And txtNameEN.Text.Trim = "" Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & Resources.resMsgKYC.msgCondition & "')", True)
                    GoTo checkVisible
                ElseIf txtCustCode.Text.Trim = "*" Or txtBizCode.Text.Trim = "*" Or txtNameTH.Text.Trim = "*" Or txtNameEN.Text.Trim = "*" Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & Resources.resMsgKYC.msgCondition & "')", True)
                    GoTo checkVisible
                End If

checkSearch:

                If Not txtBizCode.Text.Trim = "" And (Not txtNameTH.Text.Trim = "" Or Not txtNameEN.Text.Trim = "") Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & Resources.resMsgKYC.msgDoubleCondition & "')", True)

                ElseIf clsCheckLanguage.checkLanguageEN(txtNameEN.Text.Trim) = False Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & Resources.resMsgKYC.msgEN & "')", True)

                ElseIf clsCheckLanguage.checkLanguageTH(txtNameTH.Text.Trim) = False Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & Resources.resMsgKYC.msgTH & "')", True)
                Else
                    IsDataBind()
                    btnAdd.Enabled = True
                End If
            Else
                GoTo checkSearch
                IsDataBind()
                btnAdd.Enabled = True
            End If

checkVisible:
            VisbleKYC()
            clsCheckLanguage = Nothing

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub btnAddOld_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddOld.Click
        Try
            Dim index As Integer = grdNonIndividual.SelectedIndex
            If index > -1 Then 'ลูกค้าเก่า - เปิดบัญชีใหม่
                SaveLogSearch(index)

                Response.Redirect("NonIndividualCDD.aspx?KYC=" & Request.QueryString("KYC") & "&TypeKYC=2&CUSTSEQ=" & CInt(grdNonIndividual.Rows(index).Cells(0).Text) & "")
            Else
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & Resources.resMsgKYC.msgSelect & "')", True)
            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub btnKYC_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnKYC.Click
        Try

            Dim index As Integer = grdNonIndividual.SelectedIndex
            If Request.QueryString("KYC") = 1 Then
                If index = -1 Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC1T_02.aspx','_blank','scrollbars=1,Width:800px, Height:600px')", True)
                Else
                    SaveLogPrint(index)
                    Dim custtype As String = DirectCast(grdNonIndividual.Rows(index).FindControl("lblCustCode"), Label).Text
                    If custtype = "ลูกค้าขาจร" Then
                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC5T_02.aspx?CUSTSEQ=" & CInt(grdNonIndividual.Rows(index).Cells(0).Text) & "','_blank','scrollbars=1,Width:1000px, Height:600px')", True)
                    Else
                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC1T_02.aspx?CUSTSEQ=" & CInt(grdNonIndividual.Rows(index).Cells(0).Text) & "&Type=" & Request.QueryString("KYC") & "&KYCSEQ=0','_blank','scrollbars=1,Width:1000px, Height:600px')", True)
                    End If
                End If
            ElseIf Request.QueryString("KYC") = 2 Then
                If index = -1 Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC2T_02.aspx','_blank','scrollbars=1,Width:800px, Height:600px')", True)
                Else
                    SaveLogPrint(index)
                    Dim custtype As String = DirectCast(grdNonIndividual.Rows(index).FindControl("lblCustCode"), Label).Text
                    If custtype = "ลูกค้าขาจร" Then
                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC5T_02.aspx?CUSTSEQ=" & CInt(grdNonIndividual.Rows(index).Cells(0).Text) & "','_blank','scrollbars=1,Width:1000px, Height:600px')", True)
                    Else
                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC2T_02.aspx?CUSTSEQ=" & CInt(grdNonIndividual.Rows(index).Cells(0).Text) & "&Type=" & Request.QueryString("KYC") & "&KYCSEQ=0','_blank','scrollbars=1,Width:1000px, Height:600px')", True)
                    End If
                End If

            ElseIf Request.QueryString("KYC") = 3 Then
                If index = -1 Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC3T_02.aspx','_blank','scrollbars=1,Width:800px, Height:600px')", True)
                Else
                    SaveLogPrint(index)
                    Dim custtype As String = DirectCast(grdNonIndividual.Rows(index).FindControl("lblCustCode"), Label).Text
                    If custtype = "ลูกค้าขาจร" Then
                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC5T_02.aspx?CUSTSEQ=" & CInt(grdNonIndividual.Rows(index).Cells(0).Text) & "','_blank','scrollbars=1,Width:1000px, Height:600px')", True)
                    Else
                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC3T_02.aspx?CUSTSEQ=" & CInt(grdNonIndividual.Rows(index).Cells(0).Text) & "&Type=" & Request.QueryString("KYC") & "&KYCSEQ=0','_blank','scrollbars=1,Width:1000px, Height:600px')", True)
                    End If
                End If
            ElseIf Request.QueryString("KYC") = 5 Then
                If index = -1 Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC4T_02.aspx?CUSTSEQ=0','_blank','scrollbars=1,Width:800px, Height:600px')", True)
                Else
                    SaveLogPrint(index)
                    Dim custtype As String = DirectCast(grdNonIndividual.Rows(index).FindControl("lblCustCode"), Label).Text
                    If custtype = "ลูกค้าขาจร" Then
                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC5T_02.aspx?CUSTSEQ=" & CInt(grdNonIndividual.Rows(index).Cells(0).Text) & "','_blank','scrollbars=1,Width:1000px, Height:600px')", True)
                    Else
                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC4T_02.aspx?CUSTSEQ=" & CInt(grdNonIndividual.Rows(index).Cells(0).Text) & "','_blank','scrollbars=1,Width:1000px, Height:600px')", True)
                    End If
                End If



            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub btnPrintD_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrintD.Click
        Try
            Dim index As Integer = grdNonIndividual.SelectedIndex

            If index = -1 Then
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC1T_02.aspx','_blank','scrollbars=1,Width:800px, Height:600px')", True)
            Else
                SaveLogPrint(index)

                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC1T_02.aspx?CUSTSEQ=" & CInt(grdNonIndividual.Rows(index).Cells(0).Text) & "&Type=1','_blank','scrollbars=1,Width:800px, Height:600px')", True)
            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub btnPrintL_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrintL.Click
        Try

            Dim index As Integer = grdNonIndividual.SelectedIndex

        If index = -1 Then
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC2T_02.aspx','_blank','scrollbars=1,Width:800px, Height:600px')", True)
        Else
            SaveLogPrint(index)

            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC2T_02.aspx?CUSTSEQ=" & CInt(grdNonIndividual.Rows(index).Cells(0).Text) & "','_blank','scrollbars=1,Width:800px, Height:600px')", True)
        End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub btnPrintP_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrintP.Click
        Try
            Dim index As Integer = grdNonIndividual.SelectedIndex

            If index = -1 Then
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC1T_02.aspx','_blank','scrollbars=1,Width:800px, Height:600px')", True)
            Else
                SaveLogPrint(index)

                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.open('../Form/KYC1T_02.aspx?CUSTSEQ=" & CInt(grdNonIndividual.Rows(index).Cells(0).Text) & "&Type=3 ','_blank','scrollbars=1,Width:800px, Height:600px')", True)
            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Try
            txtCustCode.Text = ""
            txtBizCode.Text = ""
            txtNameTH.Text = ""
            txtNameEN.Text = ""

            lblMsg.Visible = False

            btnAdd.Enabled = False
            btnAdd.Visible = True
            btnAddOld.Visible = False
            btnShow.Visible = False
            btnEdit.Visible = False

            chkUser.Checked = False

            lblTotal.Text = ""

            If ds.Tables.Contains("dsCustomerComS") Then
                ds.Tables("dsCustomerComS").Clear()
                grdNonIndividual.DataSource = ds.Tables("dsCustomerComS")
                grdNonIndividual.DataBind()
                grdNonIndividual.SelectedIndex = -1
            End If

            VisbleKYC()
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub btnShow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShow.Click
        Try
            Dim index As Integer = grdNonIndividual.SelectedIndex
            If index = -1 Then
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & Resources.resMsgKYC.msgSelect & "')", True)
            Else
                If grdNonIndividual.Rows(index).Cells(8).Text = "&nbsp;" Or grdNonIndividual.Rows(index).Cells(8).Text = "" Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & Resources.resMsgKYC.msgCDD & "')", True)
                Else
                    Response.Redirect("NonIndividualKYC.aspx?KYC=" & Request.QueryString("KYC") & "&TypeKYC=4&CUSTSEQ=" & CInt(grdNonIndividual.Rows(index).Cells(0).Text) & "&CDD=" & CInt(grdNonIndividual.Rows(index).Cells(8).Text) & "")
                End If

            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Try
            Dim index As Integer = grdNonIndividual.SelectedIndex
            If index = -1 Then
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & Resources.resMsgKYC.msgSelect & "')", True)
            Else 'ลูกค้าเก่า แก้ไขข้อมูล
                'If grdNonIndividual.Rows(index).Cells(8).Text = "&nbsp;" Or grdNonIndividual.Rows(index).Cells(8).Text = "" Then
                '    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & Resources.resMsgKYC.msgCDD & "')", True)
                'Else
                Response.Redirect("NonIndividualKYC.aspx?KYC=" & Request.QueryString("KYC") & "&TypeKYC=3&CUSTSEQ=" & CInt(grdNonIndividual.Rows(index).Cells(0).Text) & "&CDD=" & grdNonIndividual.Rows(index).Cells(8).Text & "")
                'End If
            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

#End Region

#Region " Function"

    Private Sub loadDropDown()
        Try
            clsMasterDropDown = New clsMasterDropDown
            If (clsSessionMenuRight.getRoleGroup > 79) Then
                clsMasterDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:="0", _
                                                strBranchCode:=clsSessionMenuRight.getBranchCode, _
                                                strRoleCode:=clsSessionMenuRight.getRoleGroup, _
                                                strZoneCode:=clsSessionMenuRight.getZoneCode, _
                                                selectData:=clsSessionMenuRight.getBranchCode)

            Else
                clsMasterDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=clsSessionMenuRight.getDeptCode, _
                                                            strBranchCode:=clsSessionMenuRight.getBranchCode, _
                                                            strRoleCode:=clsSessionMenuRight.getRoleGroup, _
                                                            strZoneCode:=clsSessionMenuRight.getZoneCode, _
                                                            selectData:=clsSessionMenuRight.getBranchCode)

            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Sub VisbleKYC()
        Try
            If Request.QueryString("KYC") = 4 Then
                btnEdit.Visible = False
                btnAdd.Visible = False
                btnAddOld.Visible = False
                chkUser.Visible = False

                'btnPrintD.Visible = True
                'btnPrintL.Visible = True
                'btnPrintP.Visible = True

                btnKYC.Visible = False
            Else
                btnShow.Visible = False
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub searchAuto(ByVal custSeq As Integer)
        Try
            Dim qryCust = From cust In KYCEntity.MCUSTOMER _
                      Where cust.CUSTTYPE = 2 And cust.CUSTSEQ = custSeq
                      Select cust

            If ds.Tables.Contains("dsCustomerComS") Then
                ds.Tables.Remove("dsCustomerComS")
            End If

            ds.Tables.Add(clsDataTable.LINQToDataTable(qryCust, "dsCustomerComS").Copy)

            grdNonIndividual.DataSource = ds.Tables("dsCustomerComS")
            grdNonIndividual.DataBind()
            btnAdd.Enabled = True

            If grdNonIndividual.Rows.Count = 0 Then
                lblMsg.Visible = True
                btnEdit.Visible = False
                btnShow.Visible = False
            Else
                setGrid()
                lblMsg.Visible = False
                btnEdit.Visible = True
                btnShow.Visible = True
            End If

            clsDataTable = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub IsDataBind()
        Dim UserRight As clsSessionMenuRight
        UserRight = Session("clsMenuRight")
        Try
            If ds.Tables.Contains("dsCustomerComS") Then
                ds.Tables.Clear()
            End If
          
            clsDatabase = New clsDatabase

            Dim sql As String = ""

            If chkUser.Checked = True Then
                sql = ""
                sql += " SELECT C.updatedate,                       "
                sql += "        C.updateby,                         "
                sql += "        C.CUSTCODE,                         "
                sql += "        C.CUSTSEQ,                          "
                sql += "        C.CUSTTYPE,                         "
                sql += "        C.CUSTSUBTYPE,                      "
                sql += "        C.BRANCHCODE,                       "
                sql += "        C.AMLCDD,                           "
                sql += "        C.AMLCDDOVERVIEW,                   "
                sql += "        C.THFIRSTNAME,                      "
                sql += "        C.THMIDNAME,                        "
                sql += "        C.THLASTNAME,                       "
                sql += "        C.ENPREFIX,                         "
                sql += "        C.ENFIRSTNAME,                      "
                sql += "        C.ENMIDNAME,                        "
                sql += "        C.ENLASTNAME,                       "
                sql += "        C.CUSTIDTYPE,                       "
                sql += "        C.UNIQUEID,                         "
                sql += "        C.UPDATEDATE,                       "
                sql += "        C.TRANTYPE,                         "
                sql += "        C.DEPD,                             "
                sql += "        C.DEPL,                             "
                sql += "        C.DEPP,                             "
                sql += "        C.DEPN,                             "
                sql += "        C.DEPUPDATE,                        "
                sql += "        C.NAMESEARCHTH,                     "
                sql += "        C.NAMESEARCHEN,                     "
                sql += "        C.ZONECODE,                         "
                sql += "        C.DEPTCODE,                         "
                sql += "        C.ALLCODERIGHT,                     "
                sql += "        C.ETL_STATUS,                       "
                '''''''''''''''''''''
                sql += "        C.CUST_COMPLETE_FLAG,                   "
                sql += "        C.CUSTOMER_OWNER,                       "
                sql += "        C.CIF_CBS_NO,                           "
                sql += "        C.CIF_FDR_NO,                           "
                sql += "        C.CIF_NPA_NO,                           "
                sql += "        C.NEXT_REVIEW,                          "
                sql += "        C.TYPEEVIDENCE_NO,                      "
                sql += "        C.TAX_ID                                "
                sql += "   FROM (SELECT distinct updateby ,CUSTSEQ      "
                sql += "           FROM " & System.Configuration.ConfigurationSettings.AppSettings("TableLOG_MCUSTOMER") & "  "
                sql += "          WHERE UPDATEBY ='" & clsSessionMenuRight.getUserName & "'"
                sql += "            AND UPDATEDATE >= :StartDate and UPDATEDATE <= :EndDate "
                sql += "           ) l left join " & System.Configuration.ConfigurationSettings.AppSettings("TableMCUSTOMER") & " C  "
                sql += "                      on l.CUSTSEQ = C.CUSTSEQ  "
                sql += "  where c.CUSTTYPE = 2          "

                If Request.QueryString("KYC") = 4 Then
                    If (UserRight.getRoleGroup > 79) Then 'admin ระบบขึ้นไป
                    ElseIf (UserRight.getRoleGroup > 69) Then 'Admin ฝ่าย
                        sql += " and c.DEPTCODE = '" & UserRight.getDeptCode & "'"
                    ElseIf (UserRight.getRoleGroup > 59) Then 'Admin เขต 
                        sql += " and c.DEPTCODE = '" & UserRight.getDeptCode & "' and C.ZoneCode ='" & UserRight.getZoneCode & "' "
                    ElseIf (UserRight.getRoleGroup > 49) Then 'Admin สาขา 
                        sql += " and c.DEPTCODE = '" & UserRight.getDeptCode & "' and C.BRANCHCODE='" & UserRight.getBranchCode & "' "
                    ElseIf (UserRight.getRoleGroup < 50) Then 'user ทั่วไป
                        sql += " and c.DEPTCODE = '" & UserRight.getDeptCode & "' and C.BRANCHCODE='" & UserRight.getBranchCode & "' "
                    End If
                End If
            Else
                sql = ""
                sql += " SELECT C.updatedate,                       "
                sql += "        C.updateby,                         "
                sql += "        C.CUSTCODE,                         "
                sql += "        C.CUSTSEQ,                          "
                sql += "        C.CUSTTYPE,                         "
                sql += "        C.CUSTSUBTYPE,                      "
                sql += "        C.BRANCHCODE,                       "
                sql += "        C.AMLCDD,                           "
                sql += "        C.AMLCDDOVERVIEW,                   "
                sql += "        C.THFIRSTNAME,                      "
                sql += "        C.ENFIRSTNAME,                      "
                sql += "        C.CUSTIDTYPE,                       "
                sql += "        C.UNIQUEID,                         "
                sql += "        C.UPDATEDATE,                       "
                sql += "        C.TRANTYPE ,                        "
                sql += "        C.DEPD,                             "
                sql += "        C.DEPL,                             "
                sql += "        C.DEPP,                             "
                sql += "        C.DEPN,                             "
                sql += "        C.DEPUPDATE,                        "
                sql += "        C.NAMESEARCHTH,                     "
                sql += "        C.NAMESEARCHEN,                     "
                sql += "        C.ZONECODE,                         "
                sql += "        C.DEPTCODE,                         "
                sql += "        C.ALLCODERIGHT,                     "
                sql += "        C.ETL_STATUS,                       "
                '''''''''''''''''''''
                sql += "        C.CUST_COMPLETE_FLAG,                   "
                sql += "        C.CUSTOMER_OWNER,                       "
                sql += "        C.CIF_CBS_NO,                           "
                sql += "        C.CIF_FDR_NO,                           "
                sql += "        C.CIF_NPA_NO,                           "
                sql += "        C.NEXT_REVIEW,                          "
                sql += "        C.TYPEEVIDENCE_NO,                      "
                sql += "        C.TAX_ID                                "
                sql += "   FROM " & System.Configuration.ConfigurationSettings.AppSettings("TableMCUSTOMER") & " C "
                sql += "  WHERE c.CUSTTYPE = 2                      "

                If Request.QueryString("KYC") = 4 Then
                    If (UserRight.getRoleGroup > 79) Then 'admin ระบบขึ้นไป
                    ElseIf (UserRight.getRoleGroup > 69) Then 'Admin ฝ่าย
                        sql += " and c.DEPTCODE = '" & UserRight.getDeptCode & "'"
                    ElseIf (UserRight.getRoleGroup > 59) Then 'Admin เขต 
                        sql += " and c.DEPTCODE = '" & UserRight.getDeptCode & "' and C.ZoneCode ='" & UserRight.getZoneCode & "' "
                    ElseIf (UserRight.getRoleGroup > 49) Then 'Admin สาขา
                        sql += " and c.DEPTCODE = '" & UserRight.getDeptCode & "' and c.BRANCHCODE='" & UserRight.getBranchCode & "'"
                    ElseIf (UserRight.getRoleGroup < 50) Then 'user ทั่วไป
                        sql += " and c.DEPTCODE = '" & UserRight.getDeptCode & "' and c.BRANCHCODE='" & UserRight.getBranchCode & "'"
                    End If
                End If

            End If

            If Not (txtBizCode.Text.Trim = "") Then
                'sql += " and C.UNIQUEID like '" & txtBizCode.Text.ToUpper.Trim.Replace("*", "%") & "'"
                ''''Prachak ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                sql += " and C.UNIQUEID like '%" & txtBizCode.Text.ToUpper.Trim & "%'"

            End If
            ''''Prachak ''''CIF_CBS_NO, CIF_FDR_NO, CIF_NPA_NO''''''''''''''''''''''''
            If Not (Me.txtCIF_NO.Text = "") Then
                sql += " and (CIF_CBS_NO like '%" & Me.txtCIF_NO.Text & "%'         "
                sql += "      OR CIF_FDR_NO LIKE '%" & Me.txtCIF_NO.Text & "%'      "
                sql += "      OR CIF_NPA_NO LIKE '%" & Me.txtCIF_NO.Text & "%')     "
            End If

            If Not (txtTYPEEVIDENCE_NO.Text.Trim = "") Then
                ''''Prachak ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                sql += " and C.  TYPEEVIDENCE_NO like '%" & txtTYPEEVIDENCE_NO.Text.Trim & "%'"
            End If

            If Not (txtCustCode.Text.Trim = "") Then
                If (txtCustCode.Text.Length = 12) Then
                    sql += " and  C.CUSTCODE = '" & txtCustCode.Text & "'"
                Else
                    sql += " and  C.CUSTCODE  like '" & txtCustCode.Text.ToUpper.Trim.Replace("*", "%") & "'"
                End If

            End If

            If Not (txtNameTH.Text.Trim = "") Then
                'sql += " and C.NAMESEARCHTH like '" & cutSpecialCharacter(txtNameTH.Text.Trim).Trim.Replace("*", "%") & "%'"
                ''''Prachak ''''''''''''''''''''''''''''''''''''''''''''''''''''''''';
                sql += " and C.NAMESEARCHTH like '%" & (Me.txtNameTH.Text.Trim) & "%'"
            End If

            If Not (txtNameEN.Text.Trim = "") Then
                'sql += " and UPPER(C.NAMESEARCHEN) like '" & cutSpecialCharacter(txtNameEN.Text).ToUpper.Trim.Replace("*", "%") & "'"
                sql += " and UPPER(C.NAMESEARCHEN) like '%" & (txtNameEN.Text).ToUpper.Trim & "%'"
            End If

            'If chkUser.Checked = True Then
            '    sql += " and UPDATEBY ='" & clsSessionMenuRight.getUserName & "'"
            'End If

            If Request.QueryString("KYC") = 4 Then

                sql += " and  C.UPDATEDATE >= :StartDate and C.UPDATEDATE <= :EndDate "

                If Not (ddlBranch.SelectedIndex = 0) Then
                    sql += " and C.BRANCHCODE ='" & ddlBranch.SelectedValue & "' "
                End If

            End If

            Dim cltParameter As Collection = Nothing
            Dim clsVariable As clsVariable = Nothing
            Dim clsDate As New clsDateFormat

            Dim aDate As Date = clsDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strDateIsChk:=txtStartDate.Text)
            Dim bDate As Date = clsDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strDateIsChk:=txtEndDate.Text)
            cltParameter = New Collection

            clsVariable = New clsVariable
            clsVariable.nameColumn = "StartDate"
            clsVariable.valueColumn = aDate
            cltParameter.Add(clsVariable)

            clsVariable = New clsVariable
            clsVariable.nameColumn = "EndDate"
            clsVariable.valueColumn = bDate
            cltParameter.Add(clsVariable)

            sql += " order by C.NAMESEARCHTH"

            If ds.Tables.Contains("dsCustomerComS") Then
                ds.Tables.Remove("dsCustomerComS")
            End If
            If (chkUser.Checked) Or Request.QueryString("KYC") = 4 Then
                ds.Tables.Add(clsDatabase.QueryDataSet(sql, cltParameter, "dsCustomerComS").Tables("dsCustomerComS").Copy)

            Else
                ds.Tables.Add(clsDatabase.QueryDataSet(sql, "dsCustomerComS").Tables("dsCustomerComS").Copy)

            End If
            ' ds.Tables.Add(clsDatabase.QueryDataSet(sql, "dsCustomerComS").Tables("dsCustomerComS").Copy)

            grdNonIndividual.DataSource = ds.Tables("dsCustomerComS")
            grdNonIndividual.DataBind()

            cltParameter = Nothing
            clsVariable = Nothing
            clsDate = Nothing


            If grdNonIndividual.Rows.Count = 0 Then
                lblMsg.Visible = True
                btnEdit.Visible = False
                btnShow.Visible = False
                lblTotal.Text = ""

            Else
                setGrid()
                lblMsg.Visible = False
                btnEdit.Visible = True
                btnShow.Visible = True
                lblTotal.Text = "จำนวนรายการทั้งหมด " + ds.Tables("dsCustomerComS").Rows.Count.ToString + " รายการ "

            End If

            clsDataTable = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub setGrid()
        grdNonIndividual.HeaderRow.Cells(0).Visible = False
        grdNonIndividual.HeaderRow.Cells(8).Visible = False
        For i As Integer = 0 To grdNonIndividual.Rows.Count - 1
            grdNonIndividual.Rows(i).Cells(0).Visible = False
            grdNonIndividual.Rows(i).Cells(8).Visible = False
        Next
    End Sub

    Private Function cutSpecialCharacter(ByVal bizName As String) As String
        Try
            Dim nameSearch As String = ""
            nameSearch = bizName.Trim.Replace(" ", "").Replace("&", "").Replace("'", "").Replace("(", "").Replace(")", "").Replace(",", "").Replace("-", "").Replace(".", "").Replace("""", "")

            Return nameSearch
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Sub SaveLogSearch(ByVal index As Integer)
        Try
            Dim program As String = ""
            If Request.QueryString("KYC") = 1 Then
                program = "M12"
            ElseIf Request.QueryString("KYC") = 2 Then
                program = "M22"
            ElseIf Request.QueryString("KYC") = 3 Then
                program = "M32"
            Else
                program = "M42"
            End If


            Dim desc As String = ""

            If chkUser.Checked = True Then
                desc += " ข้อมูลที่ปรับปรุงโดยผู้ใช้งาน "
            End If

            If Not (txtBizCode.Text.Trim = "") Then
                desc += " เลขทะเบียนนิติบุคคล - " + txtBizCode.Text
            End If

            If Not (txtCustCode.Text.Trim = "") Then
                desc += " เลขประจำตัวลูกค้า - " + txtCustCode.Text
            End If

            If Not (txtNameTH.Text.Trim = "") Then
                desc += " ชื่อลูกค้า (ไทย) - " + txtNameTH.Text
            End If

            If Not (txtNameEN.Text.Trim = "") Then
                desc += " ชื่อลูกค้า (อังกฤษ) - " + txtNameEN.Text
            End If

            If Not desc = "" Then
                desc = "ค้นหาข้อมูลจาก " + desc
            End If

            Dim clsLogTransaction As New clsLogTransaction
            clsLogTransaction.saveLogTransaction(grdNonIndividual.Rows(index).Cells(3).Text, clsSessionMenuRight.getUserName, 1, desc, program)
            clsLogTransaction = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub SaveLogPrint(ByVal index As Integer)
        Try
            Dim program As String = ""
            If Request.QueryString("KYC") = 1 Then
                program = "M12"
            ElseIf Request.QueryString("KYC") = 2 Then
                program = "M22"
            ElseIf Request.QueryString("KYC") = 3 Then
                program = "M32"
            Else
                program = "M42"
            End If

            Dim desc As String = "พิมพ์ฟอร์ม KYC"

            Dim clsLogTransaction As New clsLogTransaction
            clsLogTransaction.saveLogTransaction(grdNonIndividual.Rows(index).Cells(3).Text, clsSessionMenuRight.getUserName, 2, desc, program)
            clsLogTransaction = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

#End Region

#Region "Even GridView"

    Dim intBeforeItemIndex As Integer

    Protected Sub grdNonIndividual_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdNonIndividual.PageIndexChanging
        grdNonIndividual.PageIndex = e.NewPageIndex
        grdNonIndividual.SelectedIndex = -1
        grdNonIndividual.DataSource = ds.Tables("dsCustomerComS")
        grdNonIndividual.DataBind()
        setGrid()
    End Sub

    Protected Sub grdNonIndividual_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdNonIndividual.RowDataBound
        If (e.Row.RowType.ToString = "DataRow") Then
            Dim lblUpdateDate As Label = CType(e.Row.FindControl("lblUpdateDate"), Label)
            If Not (lblUpdateDate.Text = "") Then
                convertDate = New clsDateFormat
                lblUpdateDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, lblUpdateDate.Text)
                convertDate = Nothing
            End If
        End If
    End Sub

    Protected Sub grdnonIndividual_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdNonIndividual.SelectedIndexChanged
        If (grdNonIndividual.SelectedIndex = intBeforeItemIndex) Then
            grdNonIndividual.SelectedIndex = -1
            btnAdd.Visible = True
            btnAddOld.Visible = False
        End If

        VisbleKYC()
    End Sub

    Protected Sub grdnonIndividual_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdNonIndividual.SelectedIndexChanging
        intBeforeItemIndex = grdNonIndividual.SelectedIndex
        btnAdd.Visible = False
        btnAddOld.Visible = True

        VisbleKYC()
    End Sub

#End Region

   

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click

    End Sub
End Class
